// pages/share/share.js
var util = require('../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        canvasHidden: true, //设置画板的显示与隐藏，画板不隐藏会影响页面正常显示
        windowWidth: '',
        posterHeight: '',
        avatarUrl: "../../../images/shareImg.jpg",
        textName: "",
        textVivo: "",
        textImg: "",
        rwmImg: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        const res = wx.getSystemInfoSync()
        that.setData({
            screenWidth: res.windowWidth,
            screenHeight: res.windowHeight
        })

    },
    //点击开始的时间  
    timestart: function(e) {
        var _this = this;
        _this.setData({
            timestart: e.timeStamp
        });
    },
    //点击结束的时间
    timeend: function(e) {
        var _this = this;
        _this.setData({
            timeend: e.timeStamp
        });
    },
    saveImg: function(e) {
        var that = this;
        that.setData({
            canvasHidden: false
        })
        var unit = that.data.screenWidth / 375;
        var butback = "../../../images/shareImg.jpg";
        const ctx = wx.createCanvasContext('starkImg');
		ctx.drawImage(butback, 0, 0, unit * 375, unit * that.data.screenHeight);
            var txt1 = "长按识别二维码";
            var txt2 = "进入舒适快讯阅读全文";
            ctx.setFontSize(13)
            ctx.fillText(txt1, 168, 360);
            ctx.fillText(txt2, 168, 385);
            ctx.setFontSize(12)
        	ctx.fillText(' 正在舒适快讯阅读这篇文章', 78, 45);
            var chr = that.data.textVivo.split("");//这个方法是将一个字符串分割成字符串数组
            var temp = "";
            var row = [];
            ctx.setFontSize(12)
            ctx.setFillStyle("#000")
            for (var a = 0; a < chr.length; a++) {
                if (ctx.measureText(temp).width < 290) {
                    temp += chr[a];
                }
                else {
                    a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
                    row.push(temp);
                    temp = "";
                }
            }
            row.push(temp);
            //如果数组长度大于2 则截取前两个
            if (row.length > 2) {
                var rowCut = row.slice(0, 2);
                var rowPart = rowCut[1];
                var test = "";
                var empty = [];
                for (var a = 0; a < rowPart.length; a++) {
                    if (ctx.measureText(test).width < 220) {
                        test += rowPart[a];
                    }
                    else {
                        break;
                    }
                }
                empty.push(test);
                var group = empty[0] + "..."//这里只显示两行，超出的用...表示
                rowCut.splice(1, 1, group);
                row = rowCut;
            }
            for (var b = 0; b < row.length; b++) {
                ctx.fillText(row[b], 16, 100 + b * 20, 300);
            }

            ctx.arc(35, 40, 25, 0, 2 * Math.PI);
            ctx.clip();
        ctx.drawImage(that.data.avatarUrl, unit * 10, unit * 10, unit * 55, unit * 55);
        ctx.restore();
        ctx.draw();
         return false;
        //将生成好的图片保存到本地，需要延迟一会，绘制期间耗时
        setTimeout(function() {
			util.showLoading('保存中');
            wx.canvasToTempFilePath({
                canvasId: 'starkImg',
                success: function(res) {
                    var tempFilePath = res.tempFilePath;
                    that.setData({
                        imagePath: tempFilePath,
                        canvasHidden: true
                    });

                    if (!res.tempFilePath) {
                        wx.showModal({
                            title: '提示',
                            content: '图片绘制中，请稍后重试',
                            showCancel: false
                        })
                    }
                    //画板路径保存成功后，调用方法吧图片保存到用户相册
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        //保存成功失败之后，都要隐藏画板，否则影响界面显示。
                        success: (res) => {
                            //  console.log(res)
                            wx.hideLoading()
                            util.showSuccess('保存成功');
                            that.setData({
                                canvasHidden: true
                            })
                        },
                        fail: (err) => {
                            // console.log(err)
                            wx.hideLoading()
                            that.setData({
                                canvasHidden: true
                            })
                        }
                    })
                },
                fail: function(res) {
                    console.log(res);
                }
            });
        }, 200);
    },
})